import os
import tkinter
from functools import partial

import chardet

from config import BASC
from def_pack.Tkkinter_widget import CreatNormalWidget
from def_pack.image_show_frame_program import show_pic_frame_program, rename_pic_way, video_pic_filter
from def_pack.normal_solve import rename_pic_name, rename_pic_bef_name, rename_and_drop_timemark
from def_pack.recover_data import RecoverData, RecoverData4


def tes_code(strab,luji):


    print(f"点击成功,{strab}")
    print(f"路径：{luji}")

def only_video_timelist(fr1,main_win):
    tframe1 = fr1.get("1.0", "end").replace("\n", "")
    picture_dir = str(tframe1).replace("\\", "/")
    redata = RecoverData4()
    textframelist = [fr1]
    redata.write_file_from_get_frame(textframelist)

    main_win.destroy()
    # tes_code("视频图像标签筛选", picture_dir)
    # return
    # 视频图像标签筛选
    video_pic_filter(picture_dir)


def choice_filter_way(fr1,fr2,fr3,main_win,cho_num=0):
    tframe1 = fr1.get("1.0", "end").replace("\n", "")
    picture_dir = str(tframe1).replace("\\", "/")
    redata = RecoverData()
    # tframe2 = fr2.get("1.0", "  end").replace("\n", "")
    # tframe3 = fr3.get("1.0", "end").replace("\n", "")
    # str_tol = f"{tframe1}\n{tframe2}\n{tframe3}"`
    # filename = f"{BASC}/data/TempFrameData.txt"
    textframelist=[fr1,fr2,fr3]
    redata.write_file_from_get_frame(textframelist)

    if len(picture_dir)>1:
        if cho_num==0:
            main_win.destroy()
            # tes_code("一般图片筛选", picture_dir)
            # return
            #一般图片筛选
            show_pic_frame_program(picture_dir)
            # PictureFilter()
        if cho_num==1:
            # tes_code("重命名图片筛选", picture_dir)
            # return

            main_win.destroy()
            # 重命名图片筛选
            rename_pic_way(picture_dir)
            # PictureFilter()
        if cho_num==2:
            main_win.destroy()
            # tes_code("视频图像标签筛选", picture_dir)
            # return
            #视频图像标签筛选
            video_pic_filter(picture_dir)
            # PictureFilter()
        if cho_num==3:

            #根据txt文件重命名图片
            tframe2 = fr2.get("1.0", "end").replace("\n", "")
            rename_text_file = str(tframe2).replace("\\", "/")
            if len(rename_text_file) > 1:
            # tes_code("根据txt文件重命名图片", picture_dir)
            # tes_code("文本路径", rename_text_file)
            # return
            #     main_win.destroy()
                rename_pic_name(rename_text_file, picture_dir)
            else:
                print("没有输入文本路径，请输入文本路径后再尝试")
        if cho_num == 4:
            #裁剪图片名前缀
            tframe3 = fr3.get("1.0", "end").replace("\n", "")

            if len(str(tframe3)) > 0:
                bef_num = int(tframe3)
            # tes_code("裁剪图片名前缀", picture_dir)
            # tes_code("裁剪图片名前缀_数字", bef_num)
            # return
            # rename_text_file = str(tframe3).replace("\\", "/")
            #     main_win.destroy()
                rename_pic_bef_name(picture_dir, bef_place=bef_num)
            else:
                print("没有输入裁剪的字数，请输入裁剪字数后再尝试")
        if cho_num == 5:
            # 根据txt文件重命名图片
            tframe2 = fr2.get("1.0", "end").replace("\n", "")
            rename_text_file = str(tframe2).replace("\\", "/")
            if len(rename_text_file) > 1:
                rename_and_drop_timemark(picture_dir, rename_text_file)
            else:
                print("没有输入文本路径，请输入文本路径后再尝试")
        if cho_num == 6:
            picture_dir = f"F:/自动生成视频/time_file"
            os.startfile(picture_dir)

    else:
        print("没有输入视频路径，请输入视频路径再尝试")


# 创建一个按钮,这书蓝底的按钮
def create_button(main_win, h_numa, l_colu_numa, text1, command, width1=6, height1=1, bg="Wheat",columnspan=1):
    if command is None:
        btn1 = tkinter.Button(main_win, text=f"{text1}", width=width1, height=height1,
                              # padx=10, pady=10,
                              bg=bg, font=("楷书", 11), fg="MediumBlue",
                              )

    else:
        btn1 = tkinter.Button(main_win, text=f"{text1}", width=width1, height=height1,
                              # padx=10, pady=10,
                              bg=bg, font=("楷书", 11), fg="MediumBlue",
                              # LightCyan，MediumBlue
                              command=command)
    btn1.grid(row=h_numa, column=l_colu_numa, padx=5, pady=10,columnspan=columnspan)
    return btn1, h_numa, l_colu_numa


class PictureFilter():
    def __init__(self):
        self.classb = CreatNormalWidget()
        self.main_win = self.classb.creat_main_win("筛选图片及标签选项", "picture.ico")
        self.textframe = self.classb.create_textframe_show2(self.main_win, 2, 1, 80, 4, columnspan=8)
        self.textframe2 = self.classb.create_textframe_show2(self.main_win, 4, 1, 80, 4, columnspan=8)
        self.textframe3 = self.classb.create_textframe_show2(self.main_win, 6, 1, 80, 4, columnspan=8)

        self.lable = self.classb.creat_lable(self.main_win, "图片所在文件夹", 1, 0, columnspan=2)
        self.lable2 = self.classb.creat_lable(self.main_win, "重命名图片的txt文件路径", 3, 0, columnspan=2)
        self.lable3 = self.classb.creat_lable(self.main_win, "裁剪图片前几位数", 5, 0, columnspan=2)
        self.__show_button()
        self.__get_tempframe_data()
        self.main_win.mainloop()

    def __show_button(self):
        self.search_button = create_button(self.main_win, 7, 1, "一般图片筛选",
                                           partial(choice_filter_way,self.textframe[0],self.textframe2[0],
                                                   self.textframe3[0],self.main_win,cho_num=0),
                                           height1=1, width1=15,
                                           bg="LightCyan")
        self.search_button = create_button(self.main_win, 7, 2, "重命名图片筛选",partial(choice_filter_way,self.textframe[0],self.textframe2[0],
                                                   self.textframe3[0],self.main_win,cho_num=1), height1=1, width1=15,
                                           bg="LightCyan")

        self.search_button = create_button(self.main_win, 7, 3, "视频图像筛选标签", partial(choice_filter_way,self.textframe[0],self.textframe2[0],
                                                   self.textframe3[0],self.main_win,cho_num=2), height1=1, width1=15,
                                           bg="LightCyan")
        self.search_button = create_button(self.main_win, 8, 1, "根据txt文件重命名图片", partial(choice_filter_way,self.textframe[0],self.textframe2[0],
                                                   self.textframe3[0],self.main_win,cho_num=3), height1=1, width1=20,
                                           bg="LightCyan",columnspan=1)
        self.search_button = create_button(self.main_win, 8, 2, "裁剪图片名前缀", partial(choice_filter_way,self.textframe[0],self.textframe2[0],
                                                   self.textframe3[0],self.main_win,cho_num=4), height1=1, width1=15,
                                           bg="LightCyan")
        self.search_button = create_button(self.main_win, 8, 3, "重命名图片后\n删掉时间戳",
                                           partial(choice_filter_way,self.textframe[0],self.textframe2[0],
                                                   self.textframe3[0],self.main_win,cho_num=5), height1=2, width1=18,
                                           bg="LightCyan")
        self.search_button = create_button(self.main_win, 9, 1, "打开\n保存视频书签文件夹",
                                           partial(choice_filter_way, self.textframe[0], self.textframe2[0],
                                                   self.textframe3[0], self.main_win, cho_num=6), height1=2, width1=18,
                                           bg="LightCyan")
    def __get_tempframe_data(self):
        listoo = [self.textframe[0], self.textframe2[0], self.textframe3[0]]
        redata=RecoverData()
        redata.write_data(listoo)




class VideoTimeListBuild():
    def __init__(self):
        self.classb = CreatNormalWidget()
        self.main_win = self.classb.creat_main_win("筛选图片及标签选项", "picture.ico")
        self.textframe = self.classb.create_textframe_show2(self.main_win, 2, 1, 80, 4, columnspan=8)
        self.lable = self.classb.creat_lable(self.main_win, "图片所在文件夹", 1, 0, columnspan=2)

        self.__show_button()
        self.__get_tempframe_data()
        self.main_win.mainloop()

    def __show_button(self):
        self.search_button = create_button(self.main_win, 7, 3, "视频图像筛选标签",
                                           partial(only_video_timelist, self.textframe[0], self.main_win), height1=1,
                                           width1=15,
                                           bg="LightCyan")
    def __get_tempframe_data(self):
        listoo = [self.textframe[0]]
        redata=RecoverData4()
        redata.write_data(listoo)


if __name__ == '__main__':

    VideoTimeListBuild()

    pass